package com.geneticAlgorithm.wines;

public class Population {
	// Holds population
    CandidatesWines[] candidatesWines;

    // Construct a population
    public Population(int populationSize, boolean initialise) {
    	candidatesWines = new CandidatesWines[populationSize];
        // If we need to initialise a population of candidates do so
        if (initialise) {
            // Loop and create individuals
            for (int i = 0; i < populationSize(); i++) {
            	CandidatesWines newCandidate = new CandidatesWines();
            	newCandidate.generateIndividual();
            	saveCandidate(i, newCandidate);
            }
        }
    }
    
    // Saves a candidate
    public void saveCandidate(int index,CandidatesWines candidatesWines) {
    	this.candidatesWines[index] = candidatesWines;
    }
    
    // Gets a candidate from population
    public CandidatesWines getCandidate(int index) {
        return candidatesWines[index];
    }

    // Gets the best candidate in the population
    public CandidatesWines getFittest() {
    	CandidatesWines fittest = candidatesWines[0];
        // Loop through individuals to find fittest
        for (int i = 1; i < populationSize(); i++) {
            if (fittest.getFitness() <= getCandidate(i).getFitness()) {
                fittest = getCandidate(i);
            }
        }
        return fittest;
    }

    // Gets population size
    public int populationSize() {
        return candidatesWines.length;
    }
}
